home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / emulator / bsvc-1.000 / bsvc-1 / bsvc-1.0.4 / src / Framework / Tools.cxx < prev    next >
C/C++ Source or Header  |  1995-07-26  |  2KB  |  92 lines

  1. ///////////////////////////////////////////////////////////////////////////////
  2. // $Id: Tools.cxx,v 1.1 1994/02/18 19:54:12 bmott Exp $
  3. ///////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Tools.cxx - This is a collection of useful routines 
  6. //
  7. //   unsigned int StringToInt(String &);
  8. //   String& IntToString(unsigned int value, int size, int field_width);
  9. //
  10. //
  11. // BSVC "A Microprocessor Simulation Framework"
  12. // Copyright (c) 1993
  13. // By: Bradford W. Mott
  14. // November 6,1993
  15. //
  16. ///////////////////////////////////////////////////////////////////////////////
  17. // $Log: Tools.cxx,v $
  18. // Revision 1.1  1994/02/18  19:54:12  bmott
  19. // Initial revision
  20. //
  21. ///////////////////////////////////////////////////////////////////////////////
  22.  
  23. #include <stdio.h>
  24.  
  25. #include "String.h"
  26. #include "Tools.hxx"
  27.  
  28. ///////////////////////////////////////////////////////////////////////////////
  29. // Convert the string to an unsigned integer
  30. ///////////////////////////////////////////////////////////////////////////////
  31. unsigned int StringToInt(String string)
  32. {
  33.   static char hex_digits[]="0123456789abcdef";
  34.   unsigned int value=0;
  35.  
  36.   // Convert the string to all lower case
  37.   string.downcase();
  38.  
  39.   for(int t=0;t<string.length();++t)
  40.   {
  41.     for(int s=0;s<16;++s)
  42.     {
  43.       if(hex_digits[s] == (char)string[t])
  44.         break;
  45.     }
  46.     if(s==16)
  47.       break;
  48.     else
  49.       value=(value << 4) + s;
  50.   }
  51.   return(value);  
  52. }
  53.  
  54. ///////////////////////////////////////////////////////////////////////////////
  55. // Convert the value to a string with the given width
  56. ///////////////////////////////////////////////////////////////////////////////
  57. String IntToString(unsigned int value, int width)
  58. {
  59.   static char hex_digits[]="0123456789abcdef";
  60.   String string;
  61.   int t,i;
  62.  
  63.   for(t=0;t<width;++t)
  64.     string+="0";
  65.  
  66.   i=string.length()-1;
  67.   for(t=0;t<width;++t)
  68.   {
  69.     string[i]=hex_digits[value&0xf];
  70.     value=value >> 4;
  71.     if((i=i-1) < 0)
  72.       break;
  73.   }
  74.   return(string);
  75. }
  76.  
  77.  
  78. ///////////////////////////////////////////////////////////////////////////////
  79. // Convert the value to a decimal string with the given width
  80. ///////////////////////////////////////////////////////////////////////////////
  81. String IntToDecimal(unsigned long value, int width)
  82. {
  83.   char buffer[80], format[20];
  84.  
  85.   sprintf(format,"%%%dld",width);
  86.   sprintf(buffer,format,value);
  87.  
  88.   String string(buffer);
  89.   return(string);
  90. }
  91.  
  92.